package com.jackyzgy.test.others;

import java.io.FileInputStream;
import java.io.IOException;

public class CheckPictureType {
	public static void main(String[] args) {
		new CheckPictureType().testFile();
	}

	public void testFile() {
		try {
			FileInputStream fs = new FileInputStream("C:\\Users\\hp\\Desktop\\201208031007061714bohuo.jpg");
			int i = fs.available(); // 得到文件大小
			byte data[] = new byte[i];
			fs.read(data);
			String type = getType(data);
			System.out.println(type);
			fs.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getType(byte[] data) {
		String type = null;
		// Png test:
		if (data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {
			type = "PNG";
			return type;
		}
		// Gif test:
		if (data[0] == 'G' && data[1] == 'I' && data[2] == 'F') {
			type = "GIF";
			return type;
		}
		// JPG test:
		if (data[6] == 'J' && data[7] == 'F' && data[8] == 'I' && data[9] == 'F') {
			type = "JPG";
			return type;
		}
		return type;
	}
	
//	public static void main(String args[]) throws IOException {
//        File inputFile = new File("C:\\Users\\Administrator\\Desktop\\QQ截图201208081122541.png");        
//        BufferedImage input = ImageIO.read(inputFile);
//         
//         
//         File outputFile = new File("C:\\Users\\Administrator\\Desktop\\QQ截图20120808112254.gif");
//         ImageIO.write(input, "GIF", outputFile);
//         
//         outputFile = new File("C:\\Users\\Administrator\\Desktop\\QQ截图20120808112254.jpeg");
//         ImageIO.write(input, "JPEG", outputFile);
//         
//         outputFile = new File("C:\\Users\\Administrator\\Desktop\\QQ截图20120808112254.jpg");
//         ImageIO.write(input, "JPG", outputFile);
//         
//         outputFile = new File("C:\\Users\\Administrator\\Desktop\\QQ截图20120808112254.png");
//         ImageIO.write(input, "PNG", outputFile);
//     }
}